#!/usr/bin/python
# -*- coding: cp1250 -*-

import glob
import string
import sys
import pepe.p_utils.cht as cht

space_fill = lambda x, y: str(x) + ((y - len(str(x))) * ' ')

def main(files_mask, all_variants='no'):
    """ 
    https://secure.wikimedia.org/wikipedia/en/wiki/Newline

        LF (Line feed, '\n', 0x0A, 10 in decimal) or 
        CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or 
        CR followed by LF (CR+LF, 0x0D 0x0A)
        ...
        Most textual Internet protocols (including HTTP, SMTP, FTP, IRC and many others) 
        mandate the use of ASCII CR+LF (0x0D 0x0A) on the protocol level, 
        but recommend that tolerant applications recognize lone LF as well.
        ...
        In programming languages: On Unix platforms, where C originated, 
        the native newline sequence is ASCII LF (0x0A), 
        so '\n' was simply defined to be that value.
    
    http://docs.python.org/release/2.3.5/whatsnew/node7.html

        Unix uses the linefeed (ASCII character 10), 
        MacOS uses the carriage return (ASCII character 13), and 
        Windows uses a two-character sequence of a carriage return plus a newline. 
        
        Opening a file with the mode 'U' or 'rU' will open a file for reading in universal 
        newline mode. All three line ending conventions will be translated to a "\n" 
        in the strings returned by the various file methods such as read() and readline(). 
        
    http://www.python.org/dev/peps/pep-0278/
    
        Universal Newline Support
    
    """

    for path_file in glob.glob(files_mask):
    
        # http://stackoverflow.com/questions/1035340/reading-binary-file-in-python
        
        CHUNKSIZE = 2000000
        
        fi = open(path_file, "rb")
        
        fname = string.split(path_file, '\x5C')[-1] + '.cech.#'
        fo = open(fname, 'wb')
        
        counts = {'x0Dx0A': 0, 'x0C': 0, 'x0D': 0, 'x0A': 0, 'x0Cx0A': 0,
                  '_x0Dx0A': 0, '_x0C': 0, '_x0D': 0, '_x0A': 0, '_x0Cx0A': 0,
                  'x0Cx0D': 0, '_x0Cx0D': 0}
    
        #try:
        bytes_read = fi.read(CHUNKSIZE)

        while bytes_read <> '':
            
            # in case characters below will be on edge of bytes_read
            bytes_read = '_' + bytes_read + '_'

            if all_variants == 'no':
                counts['x0A'] += (len(string.split(bytes_read, '\x0A')) - 1)
                counts['x0C'] += (len(string.split(bytes_read, '\x0C')) - 1)
                counts['x0D'] += (len(string.split(bytes_read, '\x0D')) - 1)
                counts['x0Cx0A'] += (len(string.split(bytes_read, '\x0C\x0A')) - 1)
                counts['x0Cx0D'] += (len(string.split(bytes_read, '\x0C\x0D')) - 1)
                counts['x0Dx0A'] += (len(string.split(bytes_read, '\x0D\x0A')) - 1)
            else:
    
                counts['_x0A'] += string.count(bytes_read, '\x0A')
                counts['_x0C'] += string.count(bytes_read, '\x0C')
                counts['_x0D'] += string.count(bytes_read, '\x0D')
                counts['_x0Cx0A'] += string.count(bytes_read, '\x0C\x0A')
                counts['_x0Cx0D'] += string.count(bytes_read, '\x0C\x0D')
                counts['_x0Dx0A'] += string.count(bytes_read, '\x0D\x0A')

            '''
            for b in bytes_read:
                process_byte(b)
            '''
            bytes_read = fi.read(CHUNKSIZE)
        #finally:
        fi.close()


        print path_file
        print 'COUNT (0x0D 0x0A): ', counts['x0Dx0A']
        print '-' * 20

        reo = ""
        for k in sorted(counts.keys()):
            if counts[k] <> 0:
                print space_fill(k, 10), '\t', space_fill(counts[k], 15),
                reo = reo + "%s\t%s" % (space_fill(k, 10), space_fill(counts[k], 15))
                
                if k == 'x0A':
                    print 'LF, Unix, 10 decimal (cht.py, wc -l ...)'
                    reo = reo + 'LF, Unix, 10 decimal (cht.py, wc -l ...)\x0D\x0A'
                elif k == 'x0D':
                    print 'CR, MacOS, 13 decimal'
                    reo = reo + 'CR, MacOS, 13 decimal\x0D\x0A'
                elif k == 'x0Dx0A':
                    print 'CR + LF, Windows'
                    reo = reo + 'CR + LF, Windows\x0D\x0A'
                else:
                    print ''
                    reo = reo + '\x0D\x0A'

        fo.write("%s\x0D\x0A%s\x0D\x0A\x0D\x0ACOUNT (0x0D 0x0A): \t%s\x0D\x0A%s\x0D\x0A%s" %
                             (
                              'pepe/p_utils/cech.py',
                              path_file,
                              counts['x0Dx0A'],
                              '-' * 25, 
                              reo,
                              )
                             )
        fo.close()

        #cht.main('MEGL_83_2003_MAR.txt.dat')
        
if __name__ == '__main__':
    """
    """
    if len(sys.argv) == 2:
        main(sys.argv[1])
    elif len(sys.argv) == 3:
        main(sys.argv[1], sys.argv[2])
    else:
        print 'Syntax error.'
